package com.itetchologyandenglish.home.controller.Api;

import com.alibaba.fastjson.annotation.JSONField;
import com.github.pagehelper.PageInfo;
import com.itetchologyandenglish.home.config.Log;
import com.itetchologyandenglish.home.enetity.AuthUser;
import com.itetchologyandenglish.home.enetity.News;
import com.itetchologyandenglish.home.enetity.Result;
import com.itetchologyandenglish.home.service.AdminService;
import com.itetchologyandenglish.home.service.NewsService;
import com.itetchologyandenglish.home.service.UserAthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Api(tags={"新闻模块"})
@RestController
@RequestMapping("/news")
public class NewsApiController {
    @Resource
    NewsService newsService;
    @Resource
    UserAthService userAthService;
    @ApiOperation("查看新闻列表 ")
    @Log("新闻资讯-根据信息查询新闻列表")
    @JSONField(format="yyyy-MM-dd HH:mm:ss") //返回时间类型
    @DateTimeFormat(pattern="yyyy-MM-dd") //接收时间类型
    @RequestMapping(value="/showallnews",produces = "application/json")
    public Result<PageInfo> showallnews(@RequestParam("pn") Integer pn, @RequestParam("newstitle") String newstitle, @RequestParam("newsdate") String newsdate, @RequestParam("limit")Integer limit) {
        PageInfo list=newsService.getNewsByNewstitleAndNewsdate(pn,newstitle,newsdate,limit,0);
        System.out.println(list.toString());
        return new Result<PageInfo>(200,"查询成功",list);
    }
    @ApiOperation("查询新闻")
    @Log("新闻资讯-根据ID查询新闻")
    @RequestMapping(value="/findNewsById",produces = "application/json")
    public Result<News> findNewsById(Integer id){
        News news=newsService.findNewsById(id);
        System.out.println(news);
        return new Result<News>(200,"查询成功",news);
    }
    @ApiOperation("更新物流新闻")
    @Log("新闻资讯-根据ID更新新闻")
    @RequestMapping(value="/updatenews",produces = "application/json")
    public Result updatenews(@RequestParam("files")CommonsMultipartFile newPicUrl, News news, HttpSession session) throws IOException {

        if(!newPicUrl.isEmpty()) {
           String path ="C:\\lc_java_idea\\SSMBS\\src\\main\\webapp\\WEB-INF\\static\\images\\news\\";
           File dir=new File(path);
           if (!dir.exists()) {
               dir.mkdirs();
           }
           String uuid = UUID.randomUUID().toString();
           String fileName=uuid+"-"+newPicUrl.getOriginalFilename();
           File file = new File(path, fileName);
//           if(file.getParentFile()!=null||!file.getParentFile().isDirectory()){
//               file.getParentFile().mkdirs();
//           }
           newPicUrl.transferTo(file);
           news.setNewPicUrl("images/news/" + fileName);
            System.out.println(news);
       }else{
           news.setNewPicUrl(newsService.findNewsById(news.getId()).getNewPicUrl());
        }
        if(newsService.updatenews(news)){
            return new Result(200,"更改成功");
        }else{
            return new Result(200,"更改失败");
        }

    }
    @ApiOperation("删除新闻")
    @Log("新闻资讯-删除新闻")
    @RequestMapping(value="/delnews",produces = "application/json")
    public Result delnews(@RequestParam("id")Integer id){
        if(newsService.delnews(id)){
            return new Result(200,"删除成功");
        }else{
            return new Result(404,"删除成功");
        }
    }
    @ApiOperation("添加新闻")
    @Log("新闻资讯-添加新闻")
    @RequestMapping(value="/savenews",produces = "application/json")
    public Result savenews(@RequestParam(value = "files",required = false)CommonsMultipartFile newPicUrl,News news,@RequestParam("writer")String writer) throws IOException {
        //String path=request.getServletContext().getRealPath("WEB-INF/static/images/news");
        System.out.println(writer);
        String path ="C:\\lc_java_idea\\SSMBS\\src\\main\\webapp\\WEB-INF\\static\\images\\news\\";
        File dir=new File(path);
        if(!dir.exists()){
            dir.mkdir();
        }
        System.out.println("名字"+news);
        String uuid = UUID.randomUUID().toString();
        String fileName=uuid+"-"+newPicUrl.getOriginalFilename();
        File file=new File(path,fileName);
        newPicUrl.transferTo(file);
        news.setNewPicUrl("images/news/" + fileName);
        System.out.println(news);
        if(newsService.savenews(news)){
            return new Result(200,"添加成功");
        }else{
            return new Result(404,"添加失败");
        }
    }
}
